package hah;

import java.util.Stack;

public class removeDuplicates {
    public String removeDuplicates(String S){
        if(S.length()==0||S.equals("")){
            return null;
        }
        char[] c=S.toCharArray();
        Stack<Character> stack=new Stack<>();
        for (int i=0;i<c.length;i++){
            if (stack.empty()||c[i]!=stack.peek()){
                stack.push(c[i]);
            }
            else if (c[i]==stack.peek()){
                stack.pop();
            }
        }
        StringBuffer s=new StringBuffer();
        while(!stack.empty()){
                s.append(stack.pop());
        }

        return s.reverse().toString();
    }
}
